﻿using MLSMooc.BO;
using MLSMooc.DAO.Interface.Search;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MLSMooc.Helper;
using Dapper;


namespace MLSMooc.DAO.Implement.Search
{
    public class SearchDAO : ISearchDAO
    {
        private static SearchDAO instance = new SearchDAO();
        private SearchDAO() { }
        public static SearchDAO Getinstance() => instance;

        private static readonly string queryField = "select p.*,t.Id as Tid,t.Section,t.Company,t.Position,t.TName  from Mooc_Project p join Mooc_Teach t on p.Pro_Lecturer = t.Id where p.isDelete=0 and p.Pro_VideoUrl<>''";
        private static readonly string queryField1 = "select c.*,p.Pro_Name as PName,t.TName,t.Company,t.Position,t.Section  from Mooc_Course c join Mooc_Project p on c.ProId=p.Id join Mooc_Teach t on c.TeachId=t.Id where c.isDelete=0 and p.isDelete=0 and c.Course_VideoUrl<>''";

        public async Task<IEnumerable<ProjectBO>> GetProMp4ByNameinfo(string Name)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryField);
                if (Name == "")
                {
                    var res = await conn.QueryAsync<ProjectBO>(sql.ToString());
                    return res;
                }
                else
                {
                    sql.Append($@" and p.Pro_Name like '%" + Name + "%'");
                    var res = await conn.QueryAsync<ProjectBO>(sql.ToString(), new { Name = Name });
                    return res;
                }

            }
        }

        public async Task<IEnumerable<CourseBO>> GetCourseMp4ByNameinfo(string Name)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryField1);
                if (Name=="")
                {
                    var res = await conn.QueryAsync<CourseBO>(sql.ToString());
                    return res;
                }
                else
                {
                    sql.Append($@" and c.Course_Name like '%"+Name+"%'");
                    var res = await conn.QueryAsync<CourseBO>(sql.ToString());
                    return res;
                }
               
            }
        }
    }
}
